package com.baseframe.sys.service.impl;


import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baseframe.common.utils.PageUtils;
import com.baseframe.sys.entity.FrameOnlineMessage;
import com.baseframe.sys.entity.FrameOu;
import com.baseframe.sys.entity.FrameUser;
import com.baseframe.sys.service.IFrameOnlineMessageService;
import com.baseframe.sys.service.dao.FrameOnlineMessageRepository;

/**
 * @author mzl
 */
@Service("FrameMessagesTotleService")
public class FrameOnlineMessageServiceImpl implements IFrameOnlineMessageService {


    @Autowired
    private FrameOnlineMessageRepository dao;
    @PersistenceContext
	private EntityManager em;

    @Override
    public List<FrameOnlineMessage> selectList() {
        return dao.findAll();
    }

	@Override
	public FrameOnlineMessage selectById(String messageguid) {
		return dao.findOne(messageguid);
	}

	@Override
	public void delete(String messageguid) {
		dao.delete(messageguid);
		
	}

	@Override
	public void insert(FrameOnlineMessage entity) {
		dao.save(entity);
		
	}

	@Override
	public void update(FrameOnlineMessage entity) {
		dao.save(entity);
	}

	@Override
	public List<FrameOnlineMessage> selectMainList(String userguid) {
		String hql="from FrameOnlineMessage t where t.targetuserguid='"+userguid+"' and t.isdelele=0 and t.status=1 order by t.createTime desc";
		return  em.createQuery(hql).setFirstResult(0).setMaxResults(3).getResultList();
	}

	@Override
	public int getUnreadCount(String userguid) {
		String hql="select count(1) from FrameOnlineMessage t where t.targetuserguid='"+userguid+"' and t.isdelele=0 and t.status=1";
		return Integer.parseInt(em.createQuery(hql).getSingleResult().toString());
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params,String userguid) {
		String fromusername = (String) params.get("fromusername");
		int pageSize = Integer.parseInt((String) params.get("limit"));
		int page = Integer.parseInt((String) params.get("page"));
		String sidx = (String) params.get("sidx");
		String order = (String) params.get("order");
		String chql = "select count(1) from FrameOnlineMessage t where targetuserguid='"+userguid+"'";
		String hql = "from FrameOnlineMessage t  where targetuserguid='"+userguid+"'";
		if (fromusername != null && !fromusername.trim().equals("")) {
			chql += " and t.fromusername = '" + fromusername + "'";
			hql += " and t.fromusername = '" + fromusername + "'";
		}
		if (sidx != null && !sidx.equals("")) {
			hql += " order by t." + sidx + " " + order;
		}
		int total = Integer.parseInt(em.createQuery(chql).getSingleResult().toString());

		List<FrameUser> userlist = em.createQuery(hql).setFirstResult((page - 1) * pageSize)
				.setMaxResults(page * pageSize).getResultList();

		PageUtils p = new PageUtils(userlist, total, pageSize, page);
		return p;
	}


	
}
